//
// Created by Semir on 2020/5/12.
//

#include <iostream>

using namespace std;

struct ListNode {
    int val;
    struct ListNode *next;
    explicit ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* linkList(int *arr, int length) {
    auto* pHead = new ListNode(arr[0]);
    auto* cNode = pHead;
    for (int i = 1; i < length; i++) {
        cNode->next = new ListNode(arr[i]);
        cNode = cNode->next;
    }
    return pHead;
}

ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    if (pListHead == nullptr) {
        return nullptr;
    }
    auto* dNode = new ListNode(-1);
    dNode->next = pListHead;
    auto* cNode = dNode;
    while (k && cNode) {
        cNode = cNode->next;
        k--;
    }
    if (cNode == nullptr) {
        return nullptr;
    }
    auto* sNode = dNode;
    while (cNode) {
        cNode = cNode->next;
        sNode = sNode->next;
    }
    return sNode;
}

int main() {
    int arr[5] = {1,3,4,3,1};
//    ListNode* pHead = linkList(arr, 5);
    ListNode* pHead = nullptr;
    ListNode* sNode = FindKthToTail(pHead, 6);
    cout << sNode << endl;
    return 0;
}